package zorg.Project;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Locate extends ListActivity {
	
	private Database dh;
	private String[] mStrings = {"Geen Gegevens beschikbaar!"};
    private Bitmap[] mWstar=new Bitmap[5];
    private Bitmap[] mNstar=new Bitmap[5];
	
	private int Type=0;
	private TextView TopText;
	private WinkelAdapter mAdapter;
	private ReactieAdapter mRAdapter;
	private Button add;
	private Button reac;
	private Button beoor;
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        MyProperties.getInstance().LocateClass=this;
        setContentView(R.layout.listing);
        
        mNstar[0] = BitmapFactory.decodeResource(this.getResources(), R.drawable.stars_1);
        mNstar[1] = BitmapFactory.decodeResource(this.getResources(), R.drawable.stars_2);
        mNstar[2] = BitmapFactory.decodeResource(this.getResources(), R.drawable.stars_3);
        mNstar[3] = BitmapFactory.decodeResource(this.getResources(), R.drawable.stars_4);
        mNstar[4] = BitmapFactory.decodeResource(this.getResources(), R.drawable.stars_5);
        
        mWstar[0] = BitmapFactory.decodeResource(this.getResources(), R.drawable.stars_1wc);
        mWstar[1] = BitmapFactory.decodeResource(this.getResources(), R.drawable.stars_2wc);
        mWstar[2] = BitmapFactory.decodeResource(this.getResources(), R.drawable.stars_3wc);
        mWstar[3] = BitmapFactory.decodeResource(this.getResources(), R.drawable.stars_4wc);
        mWstar[4] = BitmapFactory.decodeResource(this.getResources(), R.drawable.stars_5wc);
        
		this.dh = new Database(this);
		this.dh.deleteAll();
		this.dh.insertWinkel(1,"WinkelNaam1","adres1","postcode1","0111-111111",1,1);
		this.dh.insertWinkel(2,"WinkelNaam2","adres2","postcode2","0222-222222",2,2);
		this.dh.insertWinkel(3,"WinkelNaam3","adres3","postcode3","0333-333333",3,3);
		this.dh.insertWinkel(4,"WinkelNaam4","adres4","postcode4","0444-444444",4,4);
		this.dh.insertWinkel(5,"WinkelNaam5","adres5","postcode5","0555-555555",5,5);
		
		this.dh.insertBeoordeling(1, 1, 1, 1, 4, 5);
		this.dh.insertBeoordeling(2, 1, 2, 1, 3, 2);
		this.dh.insertBeoordeling(3, 1, 3, 0, 2, 3);
		this.dh.insertBeoordeling(4, 1, 4, 1, 3, 4);
		
		this.dh.insertBeoordeling(1, 2, 1, 1, 1, 2);
		
		this.dh.insertBeoordeling(1, 3, 1, 0, 3, 5);
		
		this.dh.insertBeoordeling(1, 4, 1, 1, 2, 1);
		this.dh.insertBeoordeling(2, 4, 2, 2, 3, 5);
		
		this.dh.insertBeoordeling(1, 5, 1, 0, 1, 4);
		this.dh.insertBeoordeling(2, 5, 3, 0, 2, 5);
		this.dh.insertBeoordeling(3, 5, 2, 1, 3, 4);
		
		this.dh.insertReactie(1, 1, 2, "20100602", "Jaap", "dit is een test reactie op winkel 1\ndeze reactie is bedoeld om een idee te geven over het reactie systeem.\ndus deze test is erg belangrijk voor het testen van het uiterlijk.");
		this.dh.insertReactie(2, 1, 3, "20100602", "", "dit is een 2e reactie!");
		this.dh.insertReactie(3, 1, 2, "20100602", "", "dit is een 3e reactie!");
		this.dh.insertReactie(4, 1, 3, "20100602", "", "dit is een 4e reactie!");
		this.dh.insertReactie(5, 1, 4, "20100602", "", "dit is een 5e reactie!");
		this.dh.insertReactie(6, 1, 2, "20100602", "", "dit is een 6e reactie!");
		this.dh.insertReactie(7, 1, 1, "20100602", "", "dit is een 7e reactie!");
		
		this.dh.insertReactie(1, 5, 2, "20100602", "", "dit is een 1e reactie!");
		this.dh.insertReactie(2, 5, 3, "20100602", "", "dit is een 2e reactie!");
		this.dh.insertReactie(3, 5, 2, "20100602", "", "dit is een 3e reactie!");
		this.dh.insertReactie(4, 5, 3, "20100602", "", "dit is een 88e reactie!");
		this.dh.insertReactie(5, 5, 1, "20100602", "", "dit is een 5e reactie!");
		this.dh.insertReactie(6, 5, 2, "20100602", "", "dit is een 6e reactie!");
		this.dh.insertReactie(7, 5, 1, "20100602", "", "dit is een 7e reactie!");
		

		ListView listView = getListView();

        mAdapter = new WinkelAdapter(this);
        mRAdapter = new ReactieAdapter(this);
        setListAdapter(mAdapter);
		listView.setTextFilterEnabled(true);
		
		TextView tv = (TextView)findViewById(R.id.toplijst);
		tv.setText("Geen Winkels Gevonden!");
		beoor = (Button) findViewById(R.id.Beoordeling);
		beoor.setVisibility(4);//onzichtbaar
		reac = (Button) findViewById(R.id.Reactie);
		reac.setVisibility(4);//onzichtbaar
        
        add = (Button) findViewById(R.id.vorige);
        add.setVisibility(4);//onzichtbaar
        add.setOnClickListener(new View.OnClickListener() {
        	
        public void onClick(View v) {
        	Type=0;
        	setListAdapter(mAdapter);
    		TextView tv = (TextView)findViewById(R.id.toplijst);
    		v.setVisibility(4);//onzichtbaar
    	    if (mAdapter.getCount()>0)
    	    	tv.setText("Winkels Gevonden:");
    	    else
    	    	tv.setText("Geen Winkels Gevonden!");
    		ImageView iv = (ImageView)findViewById(R.id.iconTop);
    		iv.setVisibility(4);
    		beoor.setVisibility(4);
    		reac.setVisibility(4);
        } });
    }
    
    
	public void updatePositionOnScreen(Location loc)
	{
		if (Type!=0) return;
	    MaakWinkelLijst(loc);
	}
	private void MaakWinkelLijst(Location loc)
	{
		List<String[]> names;
		mAdapter.clearWinkels();
	    names = this.dh.selectAll();
	    for (String[] name : names) {
	    	boolean toilet=false;
	    	int Sterren=0;
	    	if (name[7].equals("1")) toilet=true;
	    	try {
	    		Sterren=Integer.parseInt(name[10]);
			} catch (Exception e) {
				Sterren=0;
			}
	    	Winkels winkel = new Winkels(name[0],name[1], name[2],name[3],name[4], Sterren, toilet, Double.parseDouble(name[5]), Double.parseDouble(name[6]));
	    	mAdapter.addWinkel(winkel);
	    	WinkelsToMaps(loc,winkel.lng,winkel.lat,name[1]);
	    }
	    TextView tv = (TextView)findViewById(R.id.toplijst);
	    if (mAdapter.getCount()>0)
	    	tv.setText("Winkels Gevonden:");
	    else
	    	tv.setText("Geen Winkels Gevonden!");
		ImageView iv = (ImageView)findViewById(R.id.iconTop);
		iv.setVisibility(4);
	}
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	if (Type==0)
    	{
    		beoor.setVisibility(0);
    		reac.setVisibility(0);
    		add.setVisibility(0);//onzichtbaar
    		Winkels winkel=mAdapter.getWinkel(position);
    		TextView tv = (TextView)findViewById(R.id.toplijst);
    		tv.setText(winkel.toString());
    		ImageView iv = (ImageView)findViewById(R.id.iconTop);
    		iv.setVisibility(0);
    		if (winkel.Sterren>0)
    		{
	        	if (winkel.Toilet==false)
	        		iv.setImageBitmap(mNstar[winkel.Sterren-1]);
	        	else
	        		iv.setImageBitmap(mWstar[winkel.Sterren-1]);
    		}
    		else
    		{
    			iv.setVisibility(4);
    		}
    		
    		List<String[]> Reacts = this.dh.GetReacties(winkel.ID);
    		if (Reacts.size()==0)
    		{
    			mRAdapter.clearReacties();
    			mRAdapter.addReactie(new Reacties("Geen Reacties!", 0, "", ""));
    		}
    		else
    		{
    			mRAdapter.clearReacties();
    			
    		    for (String[] name : Reacts) {
		    	   String nameUsed="Onbekend!";
		    	   if (name[0].equals("")==false)
		    		   nameUsed=name[0];
		    	   
		    	   int Sterren=0;
			    	try {
			    		Sterren=Integer.parseInt(name[2]);
					} catch (Exception e) {
						Sterren=0;
					}
			       Reacties reactie= new Reacties(name[3], Sterren, nameUsed, name[1].substring(6, 8) +  "-" + name[1].substring(4, 6)+  "-" + name[1].substring(0, 4));
			       mRAdapter.addReactie(reactie);
			    }
    		}
    		Type=1;
    		setListAdapter(mRAdapter);
    	}
    	else if (Type==1)
    	{
    		mRAdapter.Collapse(position);
    	}
    }
    
    public class WinkelAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        
        private ArrayList<Winkels> mWinkels = new ArrayList<Winkels>();
        
        public WinkelAdapter(Context c) {
            mContext = c;
            mInflater = LayoutInflater.from(mContext);

            // Icons bound to the rows.

        }

        public int getCount() {
            return mWinkels.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public Winkels getWinkel(int position) {
            return mWinkels.get(position);
        }
        
        public View getView(int position, View convertView, ViewGroup parent) {
            // Make an ImageView to show a photo
           /* i.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));*/
            // Give it a nice background

            ViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listtexticon, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);

                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            holder.text.setText(mWinkels.get(position).toString());
            int star=mWinkels.get(position).Sterren;
            if (star>0)
            {
            	if (mWinkels.get(position).Toilet==false)
            		holder.icon.setImageBitmap(mNstar[star-1]);
            	else
            		holder.icon.setImageBitmap(mWstar[star-1]);
            }
            return convertView;
            
        }

        private Context mContext;

        public void clearWinkels() {
        	mWinkels.clear();
            notifyDataSetChanged();
        }
        
        public void addWinkel(Winkels winkel) {
            mWinkels.add(winkel);
            notifyDataSetChanged();
        }
        public void addWinkels(Winkels[] winkel) {
        	for (int i=0; i<winkel.length; i++)
        	{
        		mWinkels.add(winkel[i]);
        	}
            notifyDataSetChanged();
        }
        class ViewHolder {
            TextView text;
            ImageView icon;
        }
    }

    public class ReactieAdapter extends BaseAdapter {

        private ArrayList<Reacties> mReacties = new ArrayList<Reacties>();
        private LayoutInflater mInflater;
        
        public ReactieAdapter(Context c) {
            mContext = c;
            mInflater = LayoutInflater.from(mContext);
        }

        public int getCount() {
            return mReacties.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public Reacties getReactie(int position) {
            return mReacties.get(position);
        }
        
        public void Collapse(int position) {
    		Reacties reactie=mRAdapter.getReactie(position);
    		reactie.Collapsed=!reactie.Collapsed;
            mReacties.set(position, reactie);
            notifyDataSetChanged();
        }
        
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listtexticon, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);

                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            holder.text.setText(mReacties.get(position).toString());
            int star=mReacties.get(position).Sterren;
            if (star>0)
            {
            		holder.icon.setImageBitmap(mNstar[star-1]);
            }
            return convertView;
        }

        private Context mContext;

        public void clearReacties() {
        	mReacties.clear();
            notifyDataSetChanged();
        }
        
        public void addReactie(Reacties reactie) {
        	mReacties.add(reactie);
            notifyDataSetChanged();
        }
        public void addReacties(Reacties[] reactie) {
        	for (int i=0; i<reactie.length; i++)
        	{
        		mReacties.add(reactie[i]);
        	}
            notifyDataSetChanged();
        }
        class ViewHolder {
            TextView text;
            ImageView icon;
        }
    }
    public void WinkelsToMaps(Location loc,double lng,double lat,String naam) {
   Maps Mapsstate = MyProperties.getInstance().MapsClass;
   Mapsstate.drawObject(lng,lat,naam);
    }

}